package com.ruoyi.odorservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.odorservice.entity.Sample;
import com.ruoyi.odorservice.entity.SampleDetail;
import com.ruoyi.odorservice.entity.model.MaterialTypeValue;
import com.ruoyi.odorservice.entity.model.SampleDetailWithMaterial;
import com.ruoyi.odorservice.entity.model.SiteMaterial;
import com.ruoyi.odorservice.entity.model.SiteMaterialSummary;
import com.ruoyi.odorservice.entity.vo.SampleDetailVo;
import com.ruoyi.odorservice.entity.vo.SampleVo;
import com.ruoyi.odorservice.entity.vo.SiteMaterialVo;
import com.ruoyi.odorservice.mapper.SampleMapper;
import com.ruoyi.odorservice.service.SampleDetailService;
import com.ruoyi.odorservice.service.SampleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SampleServiceImpl extends ServiceImpl<SampleMapper, Sample> implements SampleService {
    @Autowired
    private SampleDetailService sampleDetailService;

    /**
     * 根据样本名称获取样本详情
     *
     * @param sampleName
     * @return
     */
    @Override
    public List<SampleVo> getSampleList(String sampleName) {
        List<Sample> samples = this.list(new LambdaQueryWrapper<Sample>().eq(Sample::getSiteName, sampleName));
        List<SampleVo> vos = new ArrayList<>();
        if(!samples.isEmpty()){
            samples.forEach(sample -> {
                SampleVo vo = new SampleVo();
                vo.setSampleTime(sample.getSampleTime());
                vo.setSiteName(sample.getSiteName());
                List<SampleDetail> details = sampleDetailService.list(new LambdaQueryWrapper<SampleDetail>()
                       .eq(SampleDetail::getSampleId, sample.getId()));
                vo.setSampleDetails(details);
                vos.add(vo);
            });
        }
        return vos;
    }

    /**
     * 根据样本名称物质详情
     *
     * @param sampleName
     * @return
     */
    @Override
    public List<SampleDetailVo> getSampleWithMaterialList(String sampleName) {
        List<Sample> samples = this.list(new LambdaQueryWrapper<Sample>().eq(Sample::getSiteName, sampleName));
        List<SampleDetailVo> vos = new ArrayList<>();
//        List<SampleDetailWithMaterial> materials = new ArrayList<>();
        if(!samples.isEmpty()){
            samples.forEach(sample -> {
                SampleDetailVo vo = new SampleDetailVo();
                vo.setSampleTime(sample.getSampleTime());
                vo.setSiteName(sample.getSiteName());
                List<SampleDetailWithMaterial> details = sampleDetailService.selectSampleDetailWithMaterial(sample.getId()+"");
                double pq =details.stream().filter(s->s.getMaterialName().contains("排气量")).mapToDouble(SampleDetailWithMaterial::getConcentration).sum();
                vo.setStackVelum(pq);
                //臭气浓度
                double odor = details.stream().filter(s->s.getMaterialName().contains("臭气浓度")).mapToDouble(SampleDetailWithMaterial::getConcentration).sum();
                vo.setOdor(odor);
                List<MaterialTypeValue> materialTypeValues = new ArrayList<>();
                double other = details.stream().filter(s->!s.getMaterialName().contains("臭气")&&!s.getMaterialName().contains("排气量")&&s.getMaterialType()==null).mapToDouble(SampleDetailWithMaterial::getConcentration).sum();
                materialTypeValues.add(new MaterialTypeValue("其他",other));
                Map<String, List<SampleDetailWithMaterial>> map = details.stream().filter(s->!s.getMaterialName().contains("排气量")&&s.getMaterialType()!=null).collect(Collectors.groupingBy(SampleDetailWithMaterial::getMaterialType));
                map.forEach((k,v)->{
                    double sum = v.stream().mapToDouble(SampleDetailWithMaterial::getConcentration).sum();
                    materialTypeValues.add(new MaterialTypeValue(k,sum));
                });
                vo.setMaterialTypes(materialTypeValues);
                details.forEach(s->{
                    s.setSiteName(sample.getSiteName());
                });
                vo.setSampleDetails(details);
                vos.add(vo);
//                materials.addAll(details);
            });
        }
//        Map<String,List<SampleDetailWithMaterial>> map = materials.stream()
//                .filter(s->s.getOav()!=null&&s.getOav()>0.1)
//                .collect(Collectors.groupingBy(SampleDetailWithMaterial::getMaterialName));
//        List<SiteMaterialSummary> summaries = new ArrayList<>();
//        map.forEach((k,v)->{
//            SiteMaterialSummary materialSummary = new SiteMaterialSummary();
//            // 先找平均值和最大值，再找最大值所在的点
//            double avg = v.stream().mapToDouble(SampleDetailWithMaterial::getConcentration).average().orElse(0);
//            double max = v.stream().mapToDouble(SampleDetailWithMaterial::getConcentration).max().orElse(0);
//            materialSummary.setAvgConcentration(avg);
//            materialSummary.setMaxConcentration(max);
//            materialSummary.setMaterialName(k);
//            materialSummary.setOdorCharacteristics(v.get(0).getOdorCharacteristics());
//            materialSummary.setMaterialType(v.get(0).getMaterialType());
//            materialSummary.setOav(v.get(0).getOav());
//            materialSummary.setCasNo(v.get(0).getCasNo());
//            summaries.add(materialSummary);
//        });
        return vos;
    }

    /**
     * 根据站点名称获取站点物质详情
     *
     * @param siteNames
     * @return
     */
    @Override
    public List<SiteMaterial> getSiteMaterials(List<String> siteNames) {
        return this.baseMapper.getSiteMaterials(siteNames);
    }

    /**
     * 通过点位查询某企业物质
     *
     * @param ids
     * @return
     */
    @Override
    public List<SiteMaterial> getMaterialsBySites(List<Integer> ids) {
        return this.baseMapper.getMaterialsBySites(ids);
    }
}
